(function($) {

$.prettyDate = {

	template: function(source, params) {
		if ( arguments.length == 1 )
			return function() {
				var args = $.makeArray(arguments);
				args.unshift(source);
				return $.prettyDate.template.apply( this, args );
			};
		if ( arguments.length > 2 && params.constructor != Array  ) {
			params = $.makeArray(arguments).slice(1);
		}
		if ( params.constructor != Array ) {
			params = [ params ];
		}
		$.each(params, function(i, n) {
			source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
		});
		return source;
	},

	now: function() {
		return new Date();
	},

	fromUTC: function (utcDate, isUTC) {
		if (!isUTC) {
			return utcDate;
		} else {
			var date = new Date();
			date.setUTCDate(utcDate.getDate());
			date.setUTCHours(utcDate.getHours());
			date.setUTCMonth(utcDate.getMonth());
			date.setUTCFullYear(utcDate.getFullYear());
			date.setUTCMinutes(utcDate.getMinutes());
			date.setUTCSeconds(utcDate.getSeconds());
			date.setUTCMilliseconds(utcDate.getMilliseconds());
			return date;
		}
	},

	// Takes an ISO time and returns a string representing how
	// long ago the date represents.
	format: function(time, isUTC) {
		var date = $.prettyDate.fromUTC(new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")), isUTC),
			diff = ($.prettyDate.now().getTime() - date.getTime()) / 1000,
			day_diff = Math.floor(diff / 86400);

		if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
			return;

		var messages = $.prettyDate.messages;
		return day_diff == 0 && (
				diff < 60 && messages.now ||
				diff < 120 && messages.minute ||
				diff < 3600 && messages.minutes(Math.floor( diff / 60 )) ||
				diff < 7200 && messages.hour ||
				diff < 86400 && messages.hours(Math.floor( diff / 3600 ))) ||
			day_diff == 1 && messages.yesterday ||
			day_diff < 7 && messages.days(day_diff) ||
			day_diff < 31 && messages.weeks(Math.ceil( day_diff / 7 ));
	}

};

$.prettyDate.messages = {
	now: "just now",
	minute: "1 minute ago",
	minutes: $.prettyDate.template("{0} minutes ago"),
	hour: "1 hour ago",
	hours: $.prettyDate.template("{0} hours ago"),
	yesterday: "Yesterday",
	days: $.prettyDate.template("{0} days ago"),
	weeks: $.prettyDate.template("{0} weeks ago")
};

$.fn.prettyDate = function(options) {
	options = $.extend({
		value: function() {
			return $(this).attr(options.attribute);
		},
		interval: 10000,
		attribute: "title",
		isUTC: false
	}, options);
	var elements = this;
	function format() {
		elements.each(function() {
			var date = $.prettyDate.format(options.value.apply(this), options.isUTC);
			if ( date && $(this).text() != date )
				$(this).text( date );
		});
	}
	format();
	if (options.interval)
		setInterval(format, options.interval);
	return this;
};

})(jQuery);

/*
jQuery.fn.autoGrow = function(){
	return this.each(function(){

		var colsDefault = this.cols;
		var rowsDefault = this.rows;
		var grow = function() {
			growByRef(this);
		}
		
		var growByRef = function(obj) {
			var linesCount = 0;
			var lines = obj.value.split('\n');
			
			for (var i=lines.length-1; i>=0; --i)			{
				linesCount += Math.floor((lines[i].length / colsDefault) + 1);
			}
			if (linesCount >= rowsDefault)
				obj.rows = linesCount + 1;
			else
				obj.rows = rowsDefault;
		}
		
		// Manipulations
		this.onkeyup = grow;
		this.onfocus = grow;
		this.onblur = grow;
		growByRef(this);
	});
};*/
(function(a){a.fn.autoResize=function(j){var b=a.extend({onResize:function(){},animate:true,animateDuration:150,animateCallback:function(){},extraSpace:20,limit:1000},j);this.filter('textarea').each(function(){var c=a(this).css({resize:'none','overflow-y':'hidden'}),k=c.height(),f=(function(){var l=['height','width','lineHeight','textDecoration','letterSpacing'],h={};a.each(l,function(d,e){h[e]=c.css(e)});return c.clone().removeAttr('id').removeAttr('name').css({position:'absolute',top:0,left:-9999}).css(h).attr('tabIndex','-1').insertBefore(c)})(),i=null,g=function(){f.height(0).val(a(this).val()).scrollTop(10000);var d=Math.max(f.scrollTop(),k)+b.extraSpace,e=a(this).add(f);if(i===d){return}i=d;if(d>=b.limit){a(this).css('overflow-y','');return}b.onResize.call(this);b.animate&&c.css('display')==='block'?e.stop().animate({height:d},b.animateDuration,b.animateCallback):e.height(d)};c.unbind('.dynSiz').bind('keyup.dynSiz',g).bind('keydown.dynSiz',g).bind('change.dynSiz',g)});return this}})(jQuery);